﻿Imports ACompany.OrderProcessing.Infrastructure

Public Class Item
    Inherits BusinessBase(Of Item)
    Implements IEntity

    Private _Product As Product
    Private _Qty As Integer
    Private _Price As Decimal
    Private _Order As Order

    Public Sub New()
        ' Default Constructor required for NHibernate
    End Sub

    Public Property Order() As Order
        Get
            Return _Order
        End Get
        Set(ByVal value As Order)
            _Order = value
        End Set
    End Property

    Public Property Product() As Product
        Get
            Return _Product
        End Get
        Set(ByVal value As Product)
            _Product = value
        End Set
    End Property

    Public Property Qty() As Integer
        Get
            Return _Qty
        End Get
        Set(ByVal value As Integer)
            _Qty = value
        End Set
    End Property

    Public Property Price() As Decimal
        Get
            Return _Price
        End Get
        Set(ByVal value As Decimal)
            _Price = value
        End Set
    End Property

    Protected Overrides Sub validate()

        If Not Me.Qty > 0 Then
            Me.BrokenRules.Add(New BrokenRule("Qty", "An item qty must be greater than 0."))
        End If

        If Order Is Nothing Then
            Me.BrokenRules.Add(New BrokenRule("Order", "An item must have a parent order."))
        End If

        If Product Is Nothing Then
            Me.BrokenRules.Add(New BrokenRule("Product", "An item contain a product."))
        End If

        If Not Price > 0 Then
            Me.BrokenRules.Add(New BrokenRule("Price", "The price of a product must be greater than 0."))
        End If

    End Sub
End Class
